package data.email;

import data.Statistic;
import data.Term;

/**
 * EmailStatistic adalah statistik yang dimiliki oleh sebuah term dalam 
 * dokumen email
 * @author Robert Gunawan
 */
public class EmailStatistic extends Statistic {
    // getter dan setter
    public Integer getBodyDF() {
        return bodyDF;
    }

    public Integer getBodyTF() {
        return bodyTF;
    }

    public Integer getDateDF() {
        return dateDF;
    }

    public Integer getDateTF() {
        return dateTF;
    }

    public Integer getFromDF() {
        return fromDF;
    }

    public Integer getFromTF() {
        return fromTF;
    }

    public Integer getSubjectDF() {
        return subjectDF;
    }

    public Integer getSubjectTF() {
        return subjectTF;
    }

    public Integer getToDF() {
        return toDF;
    }

    public Integer getToTF() {
        return toTF;
    }

    public Integer getUnionDF() {
        return unionDF;
    }

    public Integer getUnionTF() {
        return unionTF;
    }
    // TF dan DF dipisahkan untuk setiap field
    private Integer fromTF;
    private Integer fromDF;
    private Integer toTF;
    private Integer toDF;
    private Integer subjectTF;
    private Integer subjectDF;
    private Integer dateTF;
    private Integer dateDF;
    private Integer bodyTF;
    private Integer bodyDF;
    private Integer unionTF;
    private Integer unionDF;

    public EmailStatistic(Term term) {
        this.setTerm(term);
        fromTF = 0;
        fromDF = 0;
        toTF = 0;
        toDF = 0;
        subjectTF = 0;
        subjectDF = 0;
        dateTF = 0;
        dateDF = 0;
        bodyTF = 0;
        bodyDF = 0;
        unionTF = 0;
        unionDF = 0;
    }

    public void incFromTF() {
        incUnionTF();
        fromTF++;
    }

    public void incFromDF() {
        incUnionDF();
        fromDF++;
    }

    public void incToTF() {
        incUnionTF();
        toTF++;
    }

    public void incToDF() {
        incUnionDF();
        toDF++;
    }

    public void incSubjectTF() {
        incUnionTF();
        subjectTF++;
    }

    public void incSubjectDF() {
        incUnionDF();
        subjectDF++;
    }

    public void incDateTF() {
        incUnionTF();
        dateTF++;
    }

    public void incDateDF() {
        incUnionDF();
        dateDF++;
    }

    public void incBodyTF() {
        incUnionTF();
        bodyTF++;
    }

    public void incBodyDF() {
        incUnionDF();
        bodyDF++;
    }

    private void incUnionTF() {
        unionTF++;
    }

    private void incUnionDF() {
        unionDF++;
    }

    @Override
    public void merge(Statistic anotherStatistic) {
        EmailStatistic statistic = (EmailStatistic) anotherStatistic;
        this.bodyTF += statistic.getBodyTF();
        this.bodyDF += statistic.getBodyDF();
        this.dateTF += statistic.getDateTF();
        this.dateDF += statistic.getDateDF();
        this.fromTF += statistic.getFromTF();
        this.fromDF += statistic.getFromDF();
        this.subjectTF += statistic.getSubjectTF();
        this.subjectDF += statistic.getSubjectDF();
        this.toTF += statistic.getToTF();
        this.toDF += statistic.getToDF();
        this.unionTF += statistic.getUnionTF();
        this.unionDF += statistic.getUnionDF();
    }

    @Override
    public String toString() {
        return "BodyDF : " + bodyDF + ", BodyTF : " + bodyTF + ", DateDF : " + dateDF + ", DateTF : " + dateTF + ", FromDF : " + fromDF + ", FromTF : " + fromTF + ", SubjectDF : " + subjectDF + ", SubjectTF : " + subjectTF + ", ToDF : " + toDF + ", ToTF : " + toTF + ", UnionDF : " + unionDF + ", UnionTF : " + unionTF;
    }

    @Override
    public void preCheck() {
    }

    @Override
    public void postCheck() {
        if (bodyTF > 0) {
            bodyDF = 1;
        }
        if (dateTF > 0) {
            dateDF = 1;
        }
        if (fromTF > 0) {
            fromDF = 1;
        }
        if (subjectTF > 0) {
            subjectDF = 1;
        }
        if (toTF > 0) {
            toDF = 1;
        }
        if (unionTF > 0) {
            unionDF = 1;
        }
    }

    public Float getFromWeight() {
        if (this.getFromDF() == 0) {
            return new Float(0);
        }
        return new Float(((float) this.getFromTF() / (float) this.getTerm().getCollectionRef().getDocumentCount()) * (Math.log10((float) this.getTerm().getCollectionRef().getDocumentCount() / (float) this.getFromDF())));
    }

    public Float getToWeight() {
        if (this.getToDF() == 0) {
            return new Float(0);
        }
        return new Float(((float) this.getToTF() / (float) this.getTerm().getCollectionRef().getDocumentCount()) * (Math.log10((float) this.getTerm().getCollectionRef().getDocumentCount() / (float) this.getToDF())));
    }

    public Float getSubjectWeight() {
        if (this.getSubjectDF() == 0) {
            return new Float(0);
        }
        return new Float(((float) this.getSubjectTF() / (float) this.getTerm().getCollectionRef().getDocumentCount()) * (Math.log10((float) this.getTerm().getCollectionRef().getDocumentCount() / (float) this.getSubjectDF())));
    }

    public Float getDateWeight() {
        if (this.getDateDF() == 0) {
            return new Float(0);
        }
        return new Float(((float) this.getDateTF() / (float) this.getTerm().getCollectionRef().getDocumentCount()) * (Math.log10((float) this.getTerm().getCollectionRef().getDocumentCount() / (float) this.getDateDF())));
    }

    public Float getBodyWeight() {
        if (this.getBodyDF() == 0) {
            return new Float(0);
        }
        return new Float(((float) this.getBodyTF() / (float) this.getTerm().getCollectionRef().getDocumentCount()) * (Math.log10((float) this.getTerm().getCollectionRef().getDocumentCount() / (float) this.getBodyDF())));
    }

    public Float getUnionWeight() {
        if (this.getUnionDF() == 0) {
            return new Float(0);
        }
        return new Float(((float) this.getUnionTF() / (float) this.getTerm().getCollectionRef().getDocumentCount()) * (Math.log10((float) this.getTerm().getCollectionRef().getDocumentCount() / (float) this.getUnionDF())));
    }
}
